#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

#
# ABC CLIO uses a seperate login for each college. The datasources are fished out of the returned data. All sources are returned. The periods are
# set in the URL.
#
# will run for previous month and all colleges unless start date (yyyy-mm-dd), end date and collegeID passed as arguments
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Add_Delta_YM);

use avsCommon;


my ($content, $response, $request, $url);
my ($runyear, $runmonth, $runday);

my $ua = new LWP::UserAgent(keep_alive=>1);
$ua->cookie_jar( {} );

dbsetup();

# go through all logins 
do {
	doLogin ();
	
	# run once for each month in optional range. typically beg and end month the same.
	($runyear, $runmonth, $runday) = @begdate;
	while ($runyear < $endyear || $runyear == $endyear && $runmonth <= $endmonth) {

		getStats ();
		($runyear, $runmonth, $runday) = Add_Delta_YM($runyear, $runmonth, $runday, 0, 1);	# advance the month
	}

	doLogout ();
} while ($sthlogin->fetch);

dbshutdown();

exit;

# called for each logininformation row. once per college
# connects to vendor and downloads text version of stats
# uses to update all stats for each datasource for each college

sub doLogin {
#login 
	$url = 'http://www.socialstudies.abc-clio.com/Login/Login.aspx?ctl00$loginContent$username=' . $username . '&ctl00$loginContent$password=' . $password . '&__EVENTTARGET=ctl00$loginContent$submitButton&__EVENTARGUMENT=&';
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET login $url :" . $response->status_line;
	};
}

sub getStats {
	#get the text stats
	# use .abc-clio.com/StaffResources/AccountInfo.aspx to find the schoolid for new schools
	$url = "http://www.socialstudies.abc-clio.com/CustomerStats/Download.aspx?beginmonth=$runmonth&beginyear=$runyear&endmonth=$runmonth&endyear=$runyear&schoolid=$option1";

	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET test stats $url :" . $response->status_line;
	};

	$content=$response->decoded_content;

	# save contents for possible debugging
	savecontent('ABCCLIO', "$collegeid-$runyear-$runmonth", $content);

	# helps the regex trundle through datasources
	$content .= 'Web site';

	# loop through each datasource
	while ($content =~ m/Web site: (?<datasource>.*?)\x0D\x0A.*?(?<month>\d+)\/(?<year>\d{4})\s\w+\s(?<sessions>\d+).*?Hour of Day(?<pagedata>.*?)(?=Web site)/smgc) {

		my $datasource=$+{datasource};

		# extract the date and session information - only 1 month should be requested by url
		my $period = myperiod($+{year},$+{month});
		my $sessions = "$+{sessions}";

		# find the searches, this data is suspect. it appears to show page views and also, if more than one month is selected
		# it accumulates all monthly data and shows searches as a total. extracted seperately because it may not be listed
		my $pagedata = $+{pagedata};
		$pagedata =~ m/(Searches|Search)\s(?<searches>\d+)/sgc;
		my $searches = (defined $+{searches}) ? "$+{searches}" : '0';

		print "$vendorid $collegeid $period $datasource $sessions $searches\n";
		$sthstats->execute($vendorid, $collegeid, $datasource, $period, $sessions, 1)
			or die "#error: sthstats 1 execute failed";
		$sthstats->execute($vendorid, $collegeid, $datasource, $period, $searches, 2)
			or die "#error: sthstats 2 execute failed";
	}

}
	
sub doLogout {
	$url = 'http://www.socialstudies.abc-clio.com/Login/Logout.aspx';
	$response = $ua->request(GET $url);
};

#
# Data returned from ABCCLIO looks something like this...
#
# ...
# Then use the back button on your browser to return to the database.
# 
# 
# Web site: World Geography
# 
# Institution	Number of IP Log ins	Number of Log ins	Number of Page Views
# Lake Sumter Community College	NA	59	261
# 
# Month	IP Log ins	Log ins	Page Views
# 7/2008	NA	3	3
# 6/2008	NA	1	5
# 4/2008	NA	4	25
# 3/2008	NA	8	43
# 2/2008	NA	42	181
# 1/2008	NA	1	4
# 
# Hour of Day	Page Views
# 6:00 - 7:00 AM	3
# 7:00 - 8:00 AM	6
# 8:00 - 9:00 AM	158
# 9:00 - 10:00 AM	10
# 10:00 - 11:00 AM	4
# 11:00 - 12:00 AM	7
# 12:00 - 1:00 PM	1
# 1:00 - 2:00 PM	11
# 2:00 - 3:00 PM	47
# 3:00 - 4:00 PM	5
# 4:00 - 5:00 PM	8
# 6:00 - 7:00 PM	1
# 
# Section	SubSection	Page Views
# Home	Home page	74
# Research	Searches	23
# Misc	Other	42
# Analyze	--	5
# Countries	--	117
# 
# Web site: ..... 
